/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.seari.controller;

import com.alibaba.fastjson.JSON;
import com.seari.bean.GraphicPosition;
import com.seari.bean.LogBean;
import com.seari.pojo.DataGridBean;
import com.seari.pojo.MessageBean;
import com.seari.pojo.QueryGraphicPosition;
import com.seari.service.GraphicPositionService;
import com.seari.service.LogService;
import com.seari.utils.StringHelper;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *
 * @author Rainbow
 */
@Controller
@RequestMapping("dataAccess/")
public class DataAccessController
{
    @Autowired
    private LogService logService;
    
    @Autowired
    private GraphicPositionService graphicPositionService;
    
    @RequestMapping("addLog")
    @ResponseBody
    public MessageBean addLog(HttpServletRequest req)
    {
        MessageBean mb = new MessageBean();
        String userName = req.getParameter("userName");
        String type = req.getParameter("type");
        String content = req.getParameter("content");
        if(StringUtils.isBlank(content))
        {
            mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
            mb.setMessage("日志信息为空");
            return mb;
        }
        LogBean logBean = new LogBean();
        logBean.setContent(content);
        logBean.setType(type);
        logBean.setUserName(userName);
        int resultNum = logService.addLog(logBean);
        if(resultNum != -1)
        {
            mb.setCode(StringHelper.MESSAGE_TYPE_SUCCESS);
            mb.setMessage("日志添加成功");
        }else{
            mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
            mb.setMessage("日志添加失败,请重试");
        }
        return mb;
    }
    
    @RequestMapping("addGraphicPosition")
    @ResponseBody
    public MessageBean addGraphicPosition(HttpServletRequest req)
    {
        MessageBean mb  = new MessageBean();
        String jsonStr = req.getParameter("posList");
        if(StringUtils.isBlank(jsonStr))
        {
            mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
            mb.setMessage("参数为空");
            return mb;
        }
        List<GraphicPosition> lists = JSON.parseArray(jsonStr, GraphicPosition.class);
        if(CollectionUtils.isEmpty(lists))
        {
            mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
            mb.setMessage("参数为空");
            return mb;
        }
        GraphicPosition gp = lists.get(0);
        graphicPositionService.deleteGraphicPositionByName(gp.getGraphicName());
        int resultNum = graphicPositionService.addGraphicPosition(lists);
        if(resultNum < 0)
        {
            mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
            mb.setMessage("位置信息添加失败");
        }else{
            mb.setCode(StringHelper.MESSAGE_TYPE_SUCCESS);
            mb.setMessage("添加成功,共添加"+resultNum+"条记录");
        }
        return mb;
    }
    @RequestMapping("getGraphicPositionByParams")
    @ResponseBody
    public DataGridBean<GraphicPosition> getGraphicPositionByParams(HttpServletRequest req)
    {
        DataGridBean<GraphicPosition> grid = new DataGridBean<GraphicPosition>();
        String graphicName = req.getParameter("graphicName");
        QueryGraphicPosition qgp =  new QueryGraphicPosition();
        qgp.setGraphicName(graphicName);
        List<GraphicPosition> rows = graphicPositionService.getGraphicPositionByParams(qgp);
        grid.setRows(rows);
        grid.setTotal(rows.size());
        return grid;
    }
}
